package cn.edu.tju.elm.mapper;

import cn.edu.tju.core.model.VirtualWallet;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface WalletMapper extends BaseMapper<VirtualWallet> {
    
    @Select("SELECT * FROM t_wallet WHERE user_id = #{userId}")
    VirtualWallet selectByUserId(@Param("userId") Integer userId);
    
    @Update("UPDATE t_wallet SET " +
            "balance = #{balance}, " +
            "frozen_amount = #{frozenAmount}, " +
            "overdraft_amount = #{overdraftAmount}, " +
            "bonus_amount = #{bonusAmount}, " +
            "version = version + 1, " +
            "updated_at = NOW() " +
            "WHERE id = #{id} AND version = #{version}")
    int updateWithOptimisticLock(VirtualWallet wallet);
}